/**
 * created by 小卷毛, 2021-07-21
 * Copyright (c) 2021, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.rzico.sbl.viewmodel

import com.lzg.extend.BaseResponse
import com.lzg.extend.jackson.JacksonConvert
import com.lzg.okrx.adapter.FlowableBody
import com.lzy.okgo.OkGo
import com.rzico.sbl.model.*
import com.xinnuo.common.extend.ifNotEmpty
import com.xinnuo.common.helper.getTimeString
import com.xinnuo.common_ui.base.addItems
import com.xinnuo.common_ui.utils.applyFlowableSchedulers
import com.xinnuo.common_ui.utils.subscribeByFinally

/**
 * 描 述：订单统计操作相关
 * 作 者：小卷毛  2021-07-21 14:17
 * 修改描述：
 * 修 改 人：
 * 修改版本：
 */
class StatisticOrderViewModel : DialogViewModel() {

    val mOrderTypeList by lazy {
        listOf(
            FilterData("全部", "", "订单类型", true),
            FilterData("预约订单", "hope", "预约订单"),
            FilterData("超时订单", "over", "超时订单"),
            FilterData("普通订单", "common", "普通订单")
        )
    }

    val mOrderPayList by lazy {
        listOf(
            FilterData("全部", "", "支付来源", true),
            FilterData("线上", "online", "线上"),
            FilterData("报单", "offline", "报单"),
            FilterData("其他", "other", "其他")
        )
    }

    /**
     * 订单列表
     */
    fun orderList(
        page: Int,
        status: String,
        type: String,
        pay: String,
        beginDate: String,
        endDate: String,
        onSuccess: (Int, ArrayList<Any>) -> Unit = { _, _ -> },
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<OrderModel>>(BaseUrl.stsOrderDet)
            .params(
                getParams(
                    "status" to status,
                    "method" to type,
                    "source" to pay,
                    "beginDate" to beginDate.ifNotEmpty { "$it 00:00:00" },
                    "endDate" to endDate.ifNotEmpty { "$it 23:59:59" },
                    "pageStart" to page * 20,
                    "pageSize" to 20
                )
            )
            .converter(object : JacksonConvert<BaseResponse<OrderModel>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map {
                ArrayList<Any>().apply {
                    it.data.data.forEach { item ->
                        // 添加头部
                        add(OrderHeader().apply {
                            orderSN = item.sn
                            hint = "下单时间："
                            time = item.createDate.getTimeString()
                            status_hint = item.statusDescr
                        })

                        // 添加商品
                        addItems(
                            item.orderItems.onEach {
                                it.orderSN = item.sn
                                it.consignee = item.consignee.orEmpty()
                                it.phone = item.phone.orEmpty()
                            }
                        )

                        // 添加底部
                        add(OrderFooter().apply {
                            orderSN = item.sn
                            total_num = item.quantity
                            amount = item.amountPaid.orEmpty()
                        })
                    }

                    add(it.data.recordsTotal)
                }
            }
            .subscribeByFinally(
                event = this,
                onSuccess = { onSuccess(it.removeLast() as Int, it) },
                onFinally = onFinally
            )
    )

}